package top.chmin.project.cloud.sys.config.info;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.chmin.project.cloud.common.BasePlugin;
import top.chmin.project.cloud.common.Page;
import top.chmin.project.cloud.tool.DatetimeTool;

import java.util.List;


/**
 * chmin
 * 2024-01-25 14:19:00
 * cloud-code
 */
@Slf4j
@Component
public class ConfigInfoPlugin extends BasePlugin {

    public void checkSave(ConfigInfoSaveRest.P p) {
        log.debug("checkSave-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("保存对象不能为空");
        }
    }

    public void beforeSave(ConfigInfo configInfo) {
        log.debug("beforeSave-configInfo: {}", JSONUtil.toJsonStr(configInfo));
        /*###########auto#start###########*/
        configInfo.setConfigCode(codeService.getCode("0001006"));
        /*###########auto##end############*/
        /* append $pluginCodeAppender$ */

    }

    public void afterSave(ConfigInfoSaveRest.P p, ConfigInfo configInfo, int result) {
        log.debug("afterSave-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterSave-configInfo: {}", JSONUtil.toJsonStr(configInfo));
        log.debug("afterSave-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkUpdate(ConfigInfoUpdateRest.P p) {
        log.debug("checkUpdate-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("更新对象不能为空");
        }
        if (p.getId() == null) {
            throw new RuntimeException("更新对象id不能为空");
        }
    }

    public void beforeUpdate(ConfigInfo configInfo) {
        log.debug("beforeUpdate-configInfo: {}", JSONUtil.toJsonStr(configInfo));

    }

    public void afterUpdate(ConfigInfoUpdateRest.P p, ConfigInfo configInfo, int result) {
        log.debug("afterUpdate-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterUpdate-configInfo: {}", JSONUtil.toJsonStr(configInfo));
        log.debug("afterUpdate-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkDelete(ConfigInfoDeleteRest.P p) {
        log.debug("checkDelete-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("删除对象不能为空");
        }
        if (p.getId() == null) {
            throw new RuntimeException("删除对象id不能为空");
        }
    }

    public void beforeDelete(ConfigInfo configInfo) {
        log.debug("beforeDelete-configInfo: {}", JSONUtil.toJsonStr(configInfo));

    }

    public void afterDelete(ConfigInfoDeleteRest.P p, ConfigInfo configInfo, int result) {
        log.debug("afterDelete-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterDelete-configInfo: {}", JSONUtil.toJsonStr(configInfo));
        log.debug("afterDelete-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelect(ConfigInfoSelectRest.P p) {
        log.debug("checkSelect-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("查询对象不能为空");
        }
        if (p.getId() == null) {
            throw new RuntimeException("查询对象id不能为空");
        }
    }

    public void beforeSelect(ConfigInfoSelectRest.P p, ConfigInfoCondition c) {
        log.debug("beforeSelect-p: {}", JSONUtil.toJsonStr(p));
        log.debug("beforeSelect-c: {}", JSONUtil.toJsonStr(c));

    }

    public void afterSelect(ConfigInfoSelectRest.P p, ConfigInfoSelectRest.R r) {
        log.debug("afterSelect-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterSelect-result: {}", JSONUtil.toJsonStr(r));

    }

    public void checkPage(ConfigInfoPageRest.P p) {
        log.debug("checkPage-p: {}", JSONUtil.toJsonStr(p));
        if (p == null) {
            throw new RuntimeException("分页对象不能为空");
        }
    }

    public void beforePage(ConfigInfoPageRest.P p, ConfigInfoCondition c) {
        log.debug("beforePage-p: {}", JSONUtil.toJsonStr(p));
        log.debug("beforePage-c: {}", JSONUtil.toJsonStr(c));

        /*###########auto#start###########*/
        c.configCode().eq(p.getConfigCode());
        c.configName().eq(p.getConfigName());
        c.createBy().eq(p.getCreateBy());
        c.createTime().ge(p.getCreateTime() == null || p.getCreateTime().length == 0 ? null : p.getCreateTime()[0]);
        c.createTime().lt(p.getCreateTime() == null || p.getCreateTime().length < 2 ? null : DatetimeTool.nextDay(p.getCreateTime()[1]));
        c.javaPath().eq(p.getJavaPath());
        c.updateBy().eq(p.getUpdateBy());
        c.updateTime().ge(p.getUpdateTime() == null || p.getUpdateTime().length == 0 ? null : p.getUpdateTime()[0]);
        c.updateTime().lt(p.getUpdateTime() == null || p.getUpdateTime().length < 2 ? null : DatetimeTool.nextDay(p.getUpdateTime()[1]));
        c.vuePath().eq(p.getVuePath());
        /*###########auto##end############*/
        /* append $pluginBeforePageAppender$ */

    }

    public void afterPage(ConfigInfoPageRest.P p, Page<ConfigInfoPageRest.R> r) {
        log.debug("afterPage-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterPage-result: {}", JSONUtil.toJsonStr(r));

    }

    /*###########auto#start###########*/
    public void checkSelectConfigCodeOption(ConfigInfoSelectConfigCodeOptionRest.P p) {
        log.debug("checkSelectConfigCodeOption-p: {}", JSONUtil.toJsonStr(p));

    }

    public void beforeSelectConfigCodeOption(ConfigInfoSelectConfigCodeOptionRest.P p, ConfigInfoCondition c) {
        log.debug("beforeSelectConfigCodeOption-p: {}", JSONUtil.toJsonStr(p));
        log.debug("beforeSelectConfigCodeOption-c: {}", JSONUtil.toJsonStr(c));

    }

    public void afterSelectConfigCodeOption(ConfigInfoSelectConfigCodeOptionRest.P p, List<ConfigInfoSelectConfigCodeOptionRest.R> r) {
        log.debug("afterSelectConfigCodeOption-p: {}", JSONUtil.toJsonStr(p));
        log.debug("afterSelectConfigCodeOption-result: {}", JSONUtil.toJsonStr(r));

    }

    /*###########auto##end############*/
    /* append $pluginSelectOptionMethodAppender$ */
}
